import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
import employeesRoute from './modules/employees'
import departmentsRoute from './modules/departments'
import permissionsRoute from './modules/permissions'
import settingsRoute from './modules/settings'

// 存储动态路由表的
export const asyncRoutes = [
  departmentsRoute,
  settingsRoute,
  employeesRoute,
  permissionsRoute

]

// 配置路由规则  静态路由表（不需要权限可以直接访问的）
export const constantRoutes = [
  {
    path: '/import',
    name: 'import',
    component: () => import('@/views/Import/index.vue')
  },
  {
    path: '/demo',
    name: 'demo',
    component: () => import('@/views/demo/index.vue')
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true // hidden 控制是否在左侧菜单显示，true ： 不显示， false: 显示
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: 'Dashboard', icon: 'dashboard' }
    }]
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }

]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  // [...constantRoutes, ...asyncRoutes] 合并静态路由和动态路由
  routes: [...constantRoutes, ...asyncRoutes]
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
